/**
 * 开场动画微信聊天
 * 在这里有
 */
class OpenAnimation extends egret.DisplayObjectContainer {
    /** 实例化黑板 */
    private black_bg: egret.Sprite = new egret.Sprite();
    /** 微信聊天容器 */
    private wx_context;
    /** 新消息 */
    private newMassage: egret.Bitmap = new egret.Bitmap();
    /** 新消息数量 */
    private MassageNum: number = 0;

    private wxData = RES.getRes("openAnimation_json");

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }


    private init() {
        /** 1 */
        this.sceneMain();
        /** 新消息在最顶部 */
        this.stage.addChild(this.newMassage);
    }

    /** 开头黑板 */
    private sceneMain() {
        /** 添加黑板 */
        this.addChild(this.black_bg);
        this.black_bg.graphics.beginFill(0, 1);
        this.black_bg.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        this.black_bg.graphics.endFill();
        /** 文字显示对象构建部分 */
        const arr = ("我是一名普通的陕西男青年,   今天我收到了很多消息,   他们都在问我......").split("");
        const txt = new eui.Label();
        this.black_bg.addChild(txt);
        txt.width = this.stage.stageWidth * .7;
        txt.x = this.stage.stageWidth * .15;
        txt.y = this.stage.stageHeight * .3;
        txt.lineSpacing = 25;
        txt.size = 40;

        /** 打字机定时器部分 */
        const timer = new egret.Timer(130, arr.length);
        let num = 0;
        timer.addEventListener(egret.TimerEvent.TIMER, () => {
            txt.text += arr[num];
            num++;
        }, this);
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {
            this.getNewMassage();
        }, this);
        timer.start();
    }

    /** 接收新消息部分 */
    private getNewMassage() {
        this.MassageNum++;
        this.newMassage.visible = true;
        this.newMassage.texture = RES.getRes(`new_${this.MassageNum}_png`);
        Main.anchorCenter(this.newMassage);
        this.newMassage.scaleX = .8;
        this.newMassage.scaleY = .8;
        this.newMassage.y = -500;
        this.newMassage.alpha = 1;
        this.newMassage.x = this.stage.stageWidth * .5;
        egret.Tween.get(this.newMassage)
            .wait(1000, false)
            .to({ y: 0, scaleX: 1, scaleY: 1, }, 200)
            .to({ y: this.newMassage.height * .5, scaleX: 1, scaleY: 1, }, 200)
            .wait(800, false)
            .to({ y: this.newMassage.height * .5, scaleX: .9, scaleY: .9, }, 200)
            .to({ scaleX: 2, scaleY: 2, alpha: 0 }, 200)
            .call(() => {
                this.wxScene();
                this.newMassage.visible = false;
            });

    }

    /** wx聊天场景弹出 */
    private wxScene() {

        const wx_context = new WxShow(this.wxData[Params.MassageNum]);
        this.addChild(wx_context);

        Main.anchorCenter(wx_context);
        wx_context.scaleX = .5;
        wx_context.scaleY = .5;
        wx_context.x = this.stage.stageWidth * .5;
        wx_context.y = this.stage.stageHeight * .5;

        /** 定时器执行完成时 */
        wx_context.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {

            if (Params.MassageNum >= this.wxData.length - 1) return egret.Tween.get(this).wait(1000).call(() => this.backPanel());

            this.getNewMassage();
            Params.bufferTalk = 0;
            Params.MassageNum++;
        }, this);
        /** 弹出动画 */
        egret.Tween.get(wx_context)
            .to({ scaleX: 1, scaleY: 1 }, 200)
            .call(() => wx_context.timer.start());

    }


    /** 后续黑板 */
    private txt = new eui.Label();
    private txt1 = new eui.Label();
    private image = new egret.Bitmap();
    private nowBlack = 0;
    private arr = RES.getRes("openAnimation_black_json");
    private backPanel() {
        const blackPanel = new egret.Sprite();
        this.addChild(blackPanel);
        blackPanel.graphics.beginFill(0, 1);
        blackPanel.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        blackPanel.graphics.endFill();

        /** 定位容器 */
        const container = new egret.Sprite();
        blackPanel.addChild(container);
        container.y = this.stage.stageHeight * .3;

        /** 这里预设好, 后面用缓动和定时器控制这里的显示 */
        const txt = this.txt;
        container.addChild(txt);

        const txt1 = this.txt1;
        container.addChild(txt1);

        const images = this.image;
        container.addChild(images);


        this.runrun(this.arr[this.nowBlack])
    }

    runrun(data) {
        if (this.nowBlack >= this.arr.length) return console.log("over");


        this.nowBlack++;
        egret.Tween.get(this.txt)
            .wait(500)
            .call(() => {
                if (data.data[0] == undefined) return;
                this.txt.visible = true;
                this.txt.text = data.data[0].body;
                this.txt.size = 50;
                this.txt.bold = true;
                Main.anchorCenter(this.txt);
                this.txt.x = this.stage.stageWidth * .5;
            })
            .wait(500)
            .call(() => {
                if (data.data[1] == undefined) return;

                if (data.data[2] == undefined) this.txt.visible = false;

                this.txt1.visible = true;
                this.txt1.text = data.data[1].body;
                this.txt1.size = 50;
                this.txt1.bold = true;
                Main.anchorCenter(this.txt1);
                this.txt1.x = this.stage.stageWidth * .5;
                this.txt1.y = this.txt.y + this.txt.height + 50;
            })
            .wait(500)
            .call(() => {

                if (data.data[2] == undefined) return this.runrun(this.arr[this.nowBlack]), this.hideBlcak();

                this.image.visible = true;
                this.image.texture = RES.getRes(data.data[2].body);
                this.image.y = this.txt1.y + this.txt1.height + 50;
                this.image.anchorOffsetX = this.image.width * .5;
                this.image.x = this.stage.stageWidth * .5;

            })
            .wait(500)
            .call(() => {
                if (data.data[2] == undefined) return;
                if (data.screen == 4) return console.log("点击进入"), this.intoBtn();

                this.hideBlcak();
                this.runrun(this.arr[this.nowBlack]);
            })
    }


    private hideBlcak() {
        this.txt.visible = false;
        this.txt1.visible = false;
        this.image.visible = false;
    }


    /** 好了现在显示完了 给一个点击进入按钮 */
    private intoBtn() {
        const btn = Main.createBitmapByName("into_png");
        this.addChild(btn);
        Main.anchorCenter(btn);
        btn.x = this.stage.stageWidth * .5;
        btn.y = this.stage.stageHeight * .7;

        egret.Tween.get(btn, { loop: true })
            .to({ scaleX: .9, scaleY: .9 }, 100)
            .to({ scaleX: 1, scaleY: 1 }, 200);

        btn.touchEnabled = true;

        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            console.log("go");

            this.dispatchEvent(new egret.Event("go_into"));
        }, this);
    }

}



